package starfield.ui;

import starfield.modpack.web.SyncingTask;
import java.awt.Color;
import java.util.logging.Level;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class SyncingDialog extends javax.swing.JDialog {
	private SyncingTask task;
	
	public SyncingDialog(java.awt.Frame parent, boolean modal, SyncingTask task) {
		super(parent, modal);
		this.task = task;
		initComponents();
		setTitle("Modpack syncing");
		
		if (!task.getModpack().isInvalid()) {
			syncingLabel.setText("Syncing modpack '" + task.getModpack().getDisplayName() + "'...");
		}
	}
	
	public static SyncingDialog show(java.awt.Frame parent, boolean modal, SyncingTask task) {
		SyncingDialog dialog = new SyncingDialog(parent, modal, task);
		dialog.setVisible(true);
		return dialog;
	}

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        syncingLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        consoleScrollPane = new javax.swing.JScrollPane();
        consoleTextPane = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        syncingLabel.setText("Syncing modpack...");

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        consoleTextPane.setEditable(false);
        consoleScrollPane.setViewportView(consoleTextPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(consoleScrollPane)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 357, Short.MAX_VALUE)
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(syncingLabel)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(syncingLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(consoleScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
		dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
		if (confirmCancellation()) {
			okButton.setEnabled(true);
			cancelButton.setEnabled(false);
		}
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
		if (!task.isDone()) {
			if (confirmCancellation()) {
				dispose();
			}
		} else {
			dispose();
		}
    }//GEN-LAST:event_formWindowClosing

	private boolean confirmCancellation() {
		if (JOptionPane.showConfirmDialog(
				this, 
				"Cancelling a sync may corrupt files. Are you sure you want to cancel?",
				"Modpack syncing",
				JOptionPane.YES_NO_OPTION,
				JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) {
			
			task.cancel(true);
			
			return true;
		} else {
			return false;
		}
	}
	
	public void log(Object message, Level level) {
		Color color;
		
			 if (level == Level.INFO)	 color = Color.BLACK;
		else if (level == Level.WARNING) color = Color.ORANGE;
		else if (level == Level.SEVERE)	 color = new Color(139, 0, 0); // darkred
		else if (level == Level.FINE)	 color = new Color(0, 100, 0); // darkgreen
		else							 color = Color.BLACK;
			 
		StyledDocument document = consoleTextPane.getStyledDocument();

        Style style = consoleTextPane.addStyle("ColoredLoggingStyle", null);
		
        StyleConstants.setForeground(style, color);

        try {
			if (consoleTextPane.getCaretPosition() > 0) {
				document.insertString(document.getLength(), System.lineSeparator(), style);
			}
			
			document.insertString(document.getLength(), message.toString(), style);
		} catch (BadLocationException ex) { }
	}
	
	public void clearLog() {
		consoleTextPane.setText("");
	}

	public JButton getOkButton() {
		return okButton;
	}

	public JButton getCancelButton() {
		return cancelButton;
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JScrollPane consoleScrollPane;
    private javax.swing.JTextPane consoleTextPane;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel syncingLabel;
    // End of variables declaration//GEN-END:variables
}
